/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.query;

import java.util.*;

public class QueryDate implements Comparable<QueryDate> {
	public int month = -1;
	public int day = -1;
	public int year = -1;
	public long startTS = 0;
	public long endTS = 0;
	
	public QueryDate() {
	}

	public QueryDate(int month, int day, int year) {
		this.month = month;
		this.day = day;
		this.year = year;
		GregorianCalendar c = new GregorianCalendar(year, month-1, day);
		this.startTS = c.getTimeInMillis();
		c.add(Calendar.DAY_OF_MONTH, 1);
		c.add(Calendar.SECOND, -1);
		this.endTS = c.getTimeInMillis();
	}

	public int compareTo(QueryDate date) {
	    if (startTS < date.startTS)
	        return -1;
	    if (startTS > date.startTS)
	        return 1;
	    return 0;
	}

	public boolean isNext(QueryDate date) {
		Calendar c1 = Calendar.getInstance();
		c1.set(year, month, day);
		c1.add(Calendar.DAY_OF_MONTH, 1);
		Calendar c2 = Calendar.getInstance();
		c2.set(date.year, date.month, date.day);
		if ((c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR))
			&& (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH))
			&& (c1.get(Calendar.DAY_OF_MONTH) == c2.get(Calendar.DAY_OF_MONTH)))
			return true;
		return false;
	}

	public static QueryDate processDate(String dateStr) {
		try {
			String[] timestampTokens = dateStr.split(" ");
			QueryDate d = null;
			String[] tokens = timestampTokens[0].split("/");
			if (tokens.length == 3) {
				// Format is mm/dd/yyyy
				d = new QueryDate( Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1]),Integer.parseInt(tokens[2]) );
			}
			tokens = timestampTokens[0].split("-");
			if (tokens.length == 3) {
				// Format is yyyy-mm-dd
			  	d = new QueryDate( Integer.parseInt(tokens[1]), Integer.parseInt(tokens[2]),Integer.parseInt(tokens[0]) );
			}
			if (timestampTokens.length == 2) {
			    // There is a start times, should be 24HH:MM:SS
			    GregorianCalendar cal = new GregorianCalendar(d.year, d.month-1, d.day);
			    cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(tokens[0]));
			    cal.set(Calendar.MINUTE, Integer.parseInt(tokens[1]));
			    cal.set(Calendar.SECOND, Integer.parseInt(tokens[2]));
			    cal.set(Calendar.MILLISECOND, 0);
			    d.startTS = cal.getTimeInMillis();
			}
			return d;
		} catch (Exception E) {
			E.printStackTrace();
		}
		return null;
	}
	
	public static QueryDate processEndDate(String dateStr) {
		try {
			String[] timestampTokens = dateStr.split(" ");
			QueryDate d = null;
			String[] tokens = timestampTokens[0].split("/");
			if (tokens.length == 3) {
				// Format is mm/dd/yyyy
				d = new QueryDate( Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1]),Integer.parseInt(tokens[2]) );
			}
			tokens = timestampTokens[0].split("-");
			if (tokens.length == 3) {
				// Format is yyyy-mm-dd
			  	d = new QueryDate( Integer.parseInt(tokens[1]), Integer.parseInt(tokens[2]),Integer.parseInt(tokens[0]) );
			}
			if (timestampTokens.length == 2) {
			    // There is a start times, should be 24HH:MM:SS
			    GregorianCalendar cal = new GregorianCalendar(d.year, d.month-1, d.day);
			    cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(tokens[0]));
			    cal.set(Calendar.MINUTE, Integer.parseInt(tokens[1]));
			    cal.set(Calendar.SECOND, Integer.parseInt(tokens[2]));
			    cal.set(Calendar.MILLISECOND, 0);
			    d.endTS = cal.getTimeInMillis();
			}
			return d;
		} catch (Exception E) {
			E.printStackTrace();
		}
		return null;
	}

	public String toString() {
		return month + "/" + day + "/" + year;
	}

	public String toMySQLString() {
		return year + "-" + month + "-" + day;
	}

	public String toXML() {
		return "<Date start=\"" + year + "-" + month + "-" + day + "\"/>";
	}
}